﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityManagementSystem.DAL;

namespace UniversityManagementSystem.UI
{
    public partial class ViewCourse : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Department department = new Department();
                Semester semester = new Semester();
                List<Department> allDepartments = new List<Department>();
                List<Semester> allSemesters = new List<Semester>();
                allSemesters = semester.GetAllSemesters();
                allDepartments = department.GetAllDepartments();
                departmentDropDownList.DataSource = allDepartments;
                departmentDropDownList.DataTextField = "DepartmentName";
                departmentDropDownList.DataValueField = "DepartmentCode";
                departmentDropDownList.DataBind();
                semesterDropDownList.DataSource = allSemesters;
                semesterDropDownList.DataTextField = "SemesterName";
                semesterDropDownList.DataValueField = "SemesterId";
                semesterDropDownList.DataBind();
                departmentDropDownList_SelectedIndexChanged(null,null);
                semesterDropDownList_SelectedIndexChanged(null,null);
            }
        }

        protected void departmentDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                List<Course> allCoursesByDepartment = new List<Course>();
                Course course = new Course();
                allCoursesByDepartment = course.GetAllCoursesByDepartment(departmentDropDownList.SelectedValue);
                allCoursesGridView.DataSource = allCoursesByDepartment;
                allCoursesGridView.DataBind();
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Unable to load courses,Please check connection";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void semesterDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                List<Course> allCoursesBySemester = new List<Course>();
                Course course = new Course();
                allCoursesBySemester = course.GelAllCourseBySemesterView(departmentDropDownList.SelectedValue, semesterDropDownList.SelectedValue);
                allCoursesGridView.DataSource = allCoursesBySemester;
                allCoursesGridView.DataBind();
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Unable to load courses,Please check connection";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }
    }
}